package com.modules.service.sys.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.constant.CommonConstant;
import com.common.vo.Result;
import com.modules.dao.sys.DictDao;
import com.modules.dao.sys.DictSettingDao;
import com.modules.dto.sys.resp.DictEntityResp;
import com.modules.entity.sys.DictEntity;
import com.modules.entity.sys.DictSettingEntity;
import com.modules.service.sys.DictService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * service 字典
 * @author yyq
 */
@Service
public class DcitServiceImpl extends ServiceImpl<DictDao, DictEntity> implements DictService{


    @Resource
    private DictSettingDao dictSettingDao;

    /**
     * 查询所有字典
     */
	@Override
	public Result<?> getDictAll() {
		List<DictEntity> list = baseMapper.selectList(Wrappers.lambdaQuery());
		List<DictSettingEntity> setupList = dictSettingDao.selectList(new LambdaQueryWrapper<DictSettingEntity>()
				.eq(DictSettingEntity::getState, CommonConstant.ONE).orderByAsc(DictSettingEntity::getSort));

		Map<String, Object> map = MapUtil.newHashMap();
		Map<String, Object> dictMap = MapUtil.newHashMap();
		List<DictEntityResp> resList = CollUtil.newArrayList();
		for(DictEntity dict : list) {
			List<DictSettingEntity> tempList = CollUtil.newArrayList();
			for(DictSettingEntity setup : setupList) {
				if(dict.getId().equals(setup.getDid())) {
					tempList.add(setup);
				}
			}
			dictMap.put(dict.getDictCode(), tempList);
			DictEntityResp resp = new DictEntityResp();
			BeanUtil.copyProperties(dict, resp);
			resp.setSetupList(tempList);
			resList.add(resp);
		}
		map.put("dict_map", dictMap);
		map.put("dict_list", resList);
		return Result.success(map);
	}

	/**
	 * 查询某个字典的字典参数设置
	 */
	@Override
	public Result<?> selectSetting(Page<DictSettingEntity> page, Long did) {
		LambdaQueryWrapper<DictSettingEntity> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(did != null, DictSettingEntity::getDid, did);
		queryWrapper.orderByAsc(DictSettingEntity::getSort);
		IPage<DictSettingEntity> iPage = dictSettingDao.selectPage(page, queryWrapper);
		return Result.page(iPage.getRecords(), iPage.getTotal());
	}

	/**
	 * 删除单个字典设置
	 * @param id
	 * @return
	 */
	@Override
	public Result<?> removeByIdSetting(String id) {
		int delete = dictSettingDao.deleteById(id);
		return delete>0?Result.success(Result.DELETE_MSG):Result.fail(Result.FAIL_MSG);
	}

	/**
	 * 保存字典设置
	 * @param dict
	 * @return
	 */
	@Override
	public Result<?> saveOrUpdateSetting(DictSettingEntity dict) {
		if(dict.getId() == null) {
			dictSettingDao.insert(dict);
		}else {
			dictSettingDao.updateById(dict);
		}
		return Result.success();
	}

	/**
	 * 获取字典text
	 */
	@Override
	public Result<?> getDictText(String code, String value) {
		DictEntity dict = baseMapper.selectOne(new LambdaQueryWrapper<DictEntity>().eq(DictEntity::getDictCode, code));
		DictSettingEntity dictSet = dictSettingDao.selectOne(new LambdaQueryWrapper<DictSettingEntity>()
				.eq(DictSettingEntity::getState, CommonConstant.ONE)
				.eq(DictSettingEntity::getDid, dict.getId())
				.eq(DictSettingEntity::getCode, value));
		return Result.success(Result.LOAD_MSG, dictSet!=null?dictSet.getRemarks():value);
	}

	/**
	 * 获取字典所有参数
	 */
	@Override
	public Result<?> getDictSetting(String code) {
		DictEntity dict = baseMapper.selectOne(new LambdaQueryWrapper<DictEntity>().eq(DictEntity::getDictCode, code));
		List<DictSettingEntity> list = dictSettingDao.selectList(new LambdaQueryWrapper<DictSettingEntity>()
				.eq(DictSettingEntity::getState, CommonConstant.ONE)
				.eq(DictSettingEntity::getDid, dict.getId()));
		return Result.success(list);
	}

	/**
	 * 修改状态
	 * @return
	 */
	@Override
	public Result<?> updateSettingState(DictSettingEntity dict) {
		dictSettingDao.updateById(dict);
		return Result.success();
	}
	

	
	

}
